<template>
  <div id="stackBar">
    <div ref="chart01" class="chart01"></div>
  </div>
</template>

<script>
export default {
  name: "stackBar",
  components: {},
  props: {
    xData: Array | Object,
    y1Data: Array | Object,
    series1: Array | Object,
    series2: Array | Object,
    series3: Array | Object,
    series4: Array | Object,
    series5: Array | Object,
  },
  data() {
    return {};
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.drawStackBar();
  },

  methods: {
    drawStackBar() {
      let that = this;
      let mychart = that.$echarts.init(that.$refs.chart01);
      let option = {
        tooltip: {
          confine: true,
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "line", // 默认为直线，可选为：'line' | 'shadow'
            lineStyle: {
              color: "#e5e5e5"
            }
          },
          formatter: function (params) {
            let resultName1 = "";
            let resultValue1 = "";
            let resultName2 = "";
            let resultValue2 = "";
            let resultName3 = "";
            let resultValue3 = "";
            let resultName4 = "";
            let resultValue4 = "";
            let result01 = "";
            params.forEach(function (item) {
              if (item.seriesName == "商品总数") {
                resultName1 = item.marker + " " + item.seriesName;
                resultValue1 = that.format_num(item.value);
              } else if (item.seriesName == "动销商品数") {
                resultName2 = item.marker + " " + item.seriesName;
                resultValue2 = that.format_num(item.value);
              }
              else if (item.seriesName == "抖音浏览总量") {
                resultName3 = item.marker + " " + item.seriesName;
                resultValue3 = that.format_num(item.value);
              } else if (item.seriesName == "动销商品浏览量") {
                resultName4 = item.marker + " " + item.seriesName;
                resultValue4 = that.format_num(item.value);
              }
              else {
                result01 +=
                  item.marker +
                  " " +
                  item.seriesName +
                  "&nbsp&nbsp&nbsp&nbsp" +
                  that.format_num(item.value) +
                  "</br>";
              }
            });
            let result = "";
            if (resultValue1 && resultValue2) {
              result =
                params[0].name +
                "</br>" +
                resultName1 +
                "&nbsp " +
                resultName2 +
                "&nbsp&nbsp&nbsp&nbsp" +
                resultValue1 +
                "/" +
                resultValue2 +
                // "</br>" +
                // resultName3 +
                // "&nbsp " +
                // resultName4 +
                // "&nbsp&nbsp&nbsp&nbsp" +
                // resultValue3 +
                // "/" +
                // resultValue4 +
                "</br>" +
                result01;
            } else if (!resultValue1 && resultValue2) {
              result =
                params[0].name +
                "</br>" +
                resultName1 +
                // "&nbsp " +
                resultName2 +
                "&nbsp&nbsp&nbsp&nbsp" +
                resultValue1 +
                // "/" +
                resultValue2 +
                // "</br>" +
                // resultName3 +
                // "&nbsp " +
                // resultName4 +
                // "&nbsp&nbsp&nbsp&nbsp" +
                // resultValue3 +
                // "/" +
                // resultValue4 +
                "</br>" +
                result01;
            }else if (resultValue1 && !resultValue2) {
              result =
                params[0].name +
                "</br>" +
                resultName1 +
                // "&nbsp " +
                resultName2 +
                "&nbsp&nbsp&nbsp&nbsp" +
                resultValue1 +
                // "/" +
                resultValue2 +
                // "</br>" +
                // resultName3 +
                // "&nbsp " +
                // resultName4 +
                // "&nbsp&nbsp&nbsp&nbsp" +
                // resultValue3 +
                // "/" +
                // resultValue4 +
                "</br>" +
                result01;
            }else if (!resultValue1 && !resultValue2) {
              result =
                params[0].name +
                "</br>" +
                resultName1 +
                // "&nbsp " +
                resultName2 +
                // "&nbsp&nbsp&nbsp&nbsp" +
                resultValue1 +
                // "/" +
                resultValue2 +
                // "</br>" +
                // resultName3 +
                // "&nbsp " +
                // resultName4 +
                // "&nbsp&nbsp&nbsp&nbsp" +
                // resultValue3 +
                // "/" +
                // resultValue4 +
                // "</br>" +
                result01;
            } else {
              result =
                params[0].name +
                "</br>" +
                resultName1 +
                "&nbsp " +
                resultName2 +
                "&nbsp&nbsp&nbsp&nbsp" +
                resultValue1 +
                "/" +
                resultValue2 +
                // "</br>" +
                // resultName3 +
                // "&nbsp " +
                // resultName4 +
                // "&nbsp&nbsp&nbsp&nbsp" +
                // resultValue3 +
                // "/" +
                // resultValue4 +
                "</br>" +
                result01;
            }
            return result;
          },
        },
        legend: {
            data: [
              "商品总数",
              "动销商品数",
              // "抖音浏览总量",
              // "动销商品浏览量",
              "商品总销量",
            ],
            top: 3,
            itemWidth: 10,
            itemHeight: 10,
            itemGap: 30,
            textStyle: {
              color: "#555"
            }
        },
        grid: {
          top: '35',
          left: "64",
          right: "74",
          bottom: "64",
          containLabel: false,
        },
        xAxis: [
          {
            type: "category",
            data: that.xData,
            //设置网格线颜色
            splitLine: {
              show: false,  // 控制图表轴线显示隐藏
              lineStyle: {
                color: ["#e5e5e5"],
                width: 1,
                type: "dashed",
              },
            },
            axisTick:{
              show: false,
              lineStyle: {
                type: 'solid'
              }
            },
            // x轴样式
            axisLine: {
              lineStyle: {
                type: "solid",
                color: "#e5e5e5", //左边线的颜色
                width: "1", //坐标线的宽度
              },
            },
            // x轴字体样式
            axisLabel: {
              interval: 0,
              // rotate: 40,
              margin: 14,
              show: true,
              textStyle: {
                color: "#888",
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12,
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "商品总数/动销商品数",
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, 110]
            },
            //设置网格线颜色
            splitLine: {
              show: true,
              lineStyle: {
                color: ["#e5e5e5"],
                width: 1,
                type: "dashed",
              },
            },
            
            axisTick:{
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#888",
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12,
              },
              formatter: function (value) {
                return that.format_num(value);
              },
            },
            axisLine: {
              lineStyle: {
                type: "solid",
                color: "#e5e5e5", //左边线的颜色
                width: "1", //坐标线的宽度
              },
            },
          },
          {
            type: "value",
            name: "商品总销量",
            //设置网格线颜色
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, -58]
            },
            // splitNumber: 5,
            minInterval: 450000,
            splitLine: {
              show: true,
              lineStyle: {
                color: ["#e5e5e5"],
                width: 1,
                type: "dashed",
              },
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#888",
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12,
              },
              formatter: function (value) {
                return that.format_num(value);
              },
            },
            axisTick:{
              show: false,
            },
            axisLine: {
              lineStyle: {
                type: "solid",
                color: "#e5e5e5", //右边线的颜色
                width: "1", //坐标线的宽度
              },
            },
          },
        ],
        series: [
          {
            name: "商品总数",
            type: "bar",
            // stack: "商品数",
            // barGap: "-100%" /*柱子重叠*/,
            yAxisIndex: 0,
            data: that.series1,
            itemStyle: {
              normal: {
                // color: "#1779ff",
                color: "#f4b477",
                //柱子颜色
              },
            },
            barWidth: 8, // 控制柱子的宽度
            // barGap: "-100%" /*多个并排柱子设置柱子之间的间距*/,
            // barCategoryGap: "5px" /*多个并排柱子设置柱子之间的间距*/,
          },
          {
            name: "动销商品数",
            type: "bar",
            // stack: "商品数",
            // barGap: "-100%" /*柱子重叠*/,
            yAxisIndex: 0,
            data: that.series2,
            itemStyle: {
              normal: {
                // color: "#bad7ff",
                color: "#f09072",
                //柱子颜色
              },
            },
            barWidth: 8, // 控制柱子的宽度
            barGap: "0.6" /*多个并排柱子设置柱子之间的间距*/,
          },
          // {
          //   name: "抖音浏览总量",
          //   type: "bar",
          //   stack: "浏览量",
          //   yAxisIndex: 1,
          //   data: that.series3,
          //   itemStyle: {
          //     normal: {
          //       // color: "#fd7f2c",
          //       color: "#f0935e",
          //       //柱子颜色
          //     },
          //   },
          //   barWidth: 6, // 控制柱子的宽度
          //   barGap: "1.5px" /*多个并排柱子设置柱子之间的间距*/,
          // },
          // {
          //   name: "动销商品浏览量",
          //   type: "bar",
          //   stack: "浏览量",
          //   yAxisIndex: 1,
          //   data: that.series4,
          //   itemStyle: {
          //     normal: {
          //       // color: "#ffddc6",
          //       color: "#ffc8a8",
          //       //柱子颜色
          //     },
          //   },
          //   barWidth: 6, // 控制柱子的宽度
          //   barGap: "1.5px" /*多个并排柱子设置柱子之间的间距*/,
          // },
          {
            name: "商品总销量",
            type: "bar",
            smooth: true, //true 为平滑曲线，false为直线
            data: that.series5,
            yAxisIndex: 1,
            itemStyle: {
              normal: {
                // color: "#3cdaa4",
                color: "#ed6b43",
                //柱子颜色
              },
            },
            barWidth: 8, // 控制柱子的宽度
            barGap: "0.6" /*多个并排柱子设置柱子之间的间距*/,
          },
        ],
      };
      mychart.setOption(option);
      // myChart.setOption({
      //   height:123
      // });
      //   窗口自适应
      setTimeout(() => {
        window.addEventListener("resize", eventFn);
      }, 200);

      function eventFn(){ //手动销毁监听事件写法
        mychart.resize();
      }
      
      this.$once('hook:beforeDestroy', ()=> {
        mychart.dispose();
        mychart = null;
        window.removeEventListener('resize',eventFn);
      });
    },

  },
};
</script>
<style lang='less' scoped>
#stackBar {
  width: 100%;
  height: 100%;
  margin-top: 20px;
  .chart01 {
    width: 100%;
    height: 100%;
    overflow: hidden;
    clear: both;
  }
}
</style>